/**
 * org.vz.product.collect.maintain.service.impl
 * @since : jdk 1.8
 */
package org.vz.product.collect.maintain.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vz.product.collect.maintain.dao.LoadBalancingDao;
import org.vz.product.collect.maintain.domain.LoadBalancingEntity;
import org.vz.product.collect.maintain.domain.entity.DuringTimeEntitiy;
import org.vz.product.collect.maintain.domain.entity.LoadBalancingResult;
import org.vz.product.collect.maintain.domain.entity.LoadBalancingResult.Data;
import org.vz.product.collect.maintain.service.LoadBalancingService;

/**
 * @author Morik is Yongxu.Wang 负载均衡
 * @CreateTime 2017年10月12日 15:07:29
 * @version 1.0.0
 * @description TODO
 */
@Service
public class LoadBalancingServiceImpl implements LoadBalancingService {
	@Autowired
	LoadBalancingDao loadBalancingDao;

	@Override
	public LoadBalancingResult getData(String startTime, String endTime) {
		LoadBalancingResult loadBalancingResult = new LoadBalancingResult();
		Map<String, String> map = new HashMap<String, String>();
		map.put("starTime", startTime);
		map.put("endTime", endTime);
		List<LoadBalancingEntity> select = loadBalancingDao.getSQLManager()
				.select("messagelog.getLoadBalancingData",
						LoadBalancingEntity.class, map);

		if (select != null) {
			loadBalancingResult.msg = "查询成功!!!";
			loadBalancingResult.status = 1;
			for (LoadBalancingEntity lbe : select) {
				int i = 0;
				if (loadBalancingResult.data.size() > 0) { // 循环完并没有找到相同id则直接添加
					i++;
					for (Data lbr : loadBalancingResult.data) {
						if (lbe.getIp().equals(lbr.serverIp)) {

							lbr.serverIp = lbe.getIp();
							lbr.cpu.add((double) lbe.getCpuUtilization());
							lbr.usage = lbe.getMemoryValue();
							lbr.total = lbe.getMemoryMax();
							lbr.time.add(lbe.getTime());
							if (lbr.usage != 0 && lbr.total != 0) {
								lbr.memory.add((double) lbr.usage
										/ (double) lbr.total);
							}
							if (lbe.getNetValue() != 0 && lbe.getNetMax() != 0) {
								lbr.netRate = (float) lbe.getNetValue()
										/ (float) lbe.getNetMax();
							}
							continue;
						}
						if (i == loadBalancingResult.data.size()) {
							LoadBalancingEntity l = select.get(0);
							Data d = new LoadBalancingResult().new Data();
							d.serverIp = l.getIp();
							d.cpu.add((double) lbe.getCpuUtilization());
							d.usage = l.getMemoryValue();
							d.total = l.getMemoryMax();
							d.time.add(l.getTime());
							if (d.usage != 0 && d.total != 0) {
								d.memory.add((double) d.usage
										/ (double) d.total);
							}
							if (l.getNetValue() != 0 && l.getNetMax() != 0) {
								d.netRate = (float) l.getNetValue()
										/ (float) l.getNetMax();
							}
							loadBalancingResult.data.add(d);
						}
					}
				} else {
					LoadBalancingEntity l = select.get(0);
					Data d = new LoadBalancingResult().new Data();
					d.serverIp = l.getIp();
					d.cpu.add((double) select.get(0).getCpuUtilization());
					d.usage = l.getMemoryValue();
					d.total = l.getMemoryMax();
					d.time.add(l.getTime());
					if (d.usage != 0 && d.total != 0) {
						d.memory.add((double) d.usage / (double) d.total);
					}
					if (l.getNetValue() != 0 && l.getNetMax() != 0) {
						d.netRate = (float) l.getNetValue()
								/ (float) l.getNetMax();
					}
					loadBalancingResult.data.add(d);
				}
			}
		}

		return loadBalancingResult;
	}

	@Override
	public LoadBalancingResult getDataByOneOvers() {
		LoadBalancingResult loadBalancingResult = new LoadBalancingResult();
		Map<String, String> map = new HashMap<String, String>(); // 查询条件
		List<String> x = new ArrayList<String>(); // x轴坐标
		List<DuringTimeEntitiy> dts = new ArrayList<DuringTimeEntitiy>(); // 6
																			// 个时间段
		setTime(x, dts);
		map.put("starTime", dts.get(0).endTime);
		map.put("endTime", dts.get(dts.size() - 1).startTime);

		List<LoadBalancingEntity> select = loadBalancingDao
				.getSQLManager()
				.select("messagelog.getAllData", LoadBalancingEntity.class, map);

		for (LoadBalancingEntity lb : select) {
			Data data = new LoadBalancingResult().new Data();
			for (DuringTimeEntitiy de : dts) {
				Map<String, String> map2 = new HashMap<String, String>();
				map2.put("starTime", de.startTime);
				map2.put("endTime", de.endTime);
				map2.put("ip", lb.getIp());
				// 根据 ip 查询 负载均衡上
				List<LoadBalancingEntity> select2 = loadBalancingDao
						.getSQLManager().select("messagelog.getDataByOneOverf",
								LoadBalancingEntity.class, map2);

				if (select2.size() > 0) {
					if (select2.get(0).getNetValue() == 0
							|| select2.get(0).getNetMax() == 0) {
						data.netRate = 0;
					} else {
						data.netRate = (float) select2.get(0).getNetValue()
								/ (float) select2.get(0).getNetMax();
					}

					data.total = select2.get(0).getMemoryMax();
					data.usage = data.total - select2.get(0).getMemoryValue();
					data.serverIp = lb.getIp();
					data.memory.add((double) (data.total - data.usage)
							/ (double) data.total * 100);
					data.cpu.add((double) select2.get(0).getCpuUtilization());
				} else {
					data.netRate = 0;
					data.netRate = 0;
					data.total = 0;
					data.serverIp = lb.getIp();
					data.memory.add(0d);
					data.memory.add(0d);
					data.cpu.add(0d);
				}

				data.time = x;
			}
			loadBalancingResult.data.add(data);
		}
		if (loadBalancingResult != null) {
			loadBalancingResult.msg = "查询成功！！！";
			loadBalancingResult.status = 1;
		}
		return loadBalancingResult;

	}

	private void setTime(List<String> x, List<DuringTimeEntitiy> dts) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdfs = new SimpleDateFormat("HH:mm");

		long time1 = c.getTimeInMillis();
		for (int i = 0; i < 6; i++) {
			DuringTimeEntitiy dt = new DuringTimeEntitiy();
			dt.endTime = sdf.format(time1);
			String time = sdfs.format(time1);
			x.add(time);
			time1 -= 1000 * 60 * 10;
			dt.startTime = sdf.format(time1);
			dts.add(dt);
		}
		Collections.reverse(x);
		Collections.reverse(dts);
	}
}
